[AccessSpecifier] ClassName(datatype arg1,datatype arg2,...) { Statements ---------- ---------- }
class rectangle { private int length,breadth; public void area() { int a=length*breadth; System.out.println("Area is "+a); } public void perimeter() { int p=2*(length+breadth); System.out.println("perimerter is"+p); } public void setdimension(int m,int n) { length=m; breadth=n; } } class demo { public static void main(String args[]) { rectangle a, b ; a = new rectangle(); b=new rectangle(); a.setdimension(5,7); b.setdimension(10,20); a.area(); a.perimeter(); b.area(); } }
Area is 35 Perimeter is 24 Area is 200
class Student { private int rollno; private String name; public void showData() { System.out.println("RollNo is "+rollno); System.out.println("Name is "+name); } public void setData(int rn,String nm) { rollno = rn; name = nm; } public void setData(String nm,int rn) { rollno = rn; name = nm ; } } class demo { public static void main(String args[]) { Student a = new Student(); Student b = new Student(); a.setData(4117,"Amit Jain"); b.setData("Seeta Rathi",3012); a.showData(); b.showData(); } }
RollNo is 4117 Name is Amit Jain RollNo is 3012 Name is Seeta Rathi
class Circle { private double R; public void area() { double A=3.14*R*R; System.out.println("Area is "+A); } public void Circle() { R = 1; } public void Circle(int n) { R = n; } public void Circle(double n) { R = n; } } class demo { public static void main(String args[]) { Circle a = new Circle(); Circle b = new Circle(5); Circle c = new Circle(2.5); a.area(); b.area(); c.area(); } }
Area is 3.14 Area is 78.5 Area is 19.625